package cn.chenxi.hadoopproject.weather.service.impl;

import cn.chenxi.hadoopproject.weather.mapper.WeatherMapper;
import cn.chenxi.hadoopproject.weather.pojo.dto.WeatherQuery;
import cn.chenxi.hadoopproject.weather.pojo.entity.Weather;
import cn.chenxi.hadoopproject.weather.pojo.vo.WeatherVO;
import cn.chenxi.hadoopproject.weather.service.WeatherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class WeatherServiceImpl implements WeatherService {
    @Autowired
    private WeatherMapper weatherMapper;

    @Override
    public List<WeatherVO> select(WeatherQuery weatherQuery) {
        log.debug("查询天气信息，查询条件为：{} + {}",weatherQuery.getCityName(), weatherQuery.getYear() );
        List<Weather> weathers = weatherMapper.select(weatherQuery);
        List<WeatherVO> weatherVOS = new ArrayList<>();

        for (Weather weather : weathers) {
            String info = weather.getDateYear();
            int weatherExtent = weather.getWeatherExtent();
            String[] strings = info.split("_");

            WeatherVO weatherVO = new WeatherVO();
            if (strings.length == 3) {
                weatherVO.setWeather(strings[1]);
            }
            weatherVO.setWeatherExtent(weatherExtent);
            weatherVOS.add(weatherVO);
        }
        return weatherVOS;
    }
}
